package com.starbaba.spread.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.service.CommonService;
import com.starbaba.core.service.CommonServiceSupport;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.spread.model.ChannelVO;
import com.starbaba.spread.model.getChannelModel;
import com.starbaba.spread.model.TableVO;
import com.starbaba.spread.service.channelService;

@Component(value="channelServiceImpl")
public class ChannelServiceImpl  extends CommonServiceSupport implements channelService {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5108922671733677590L;
	
	@Override
	public List<TableVO> getDataList(int iDisplayStart, int iDisplayLength,
			getChannelModel model, PageResponse pageResponse , CommonService server) {
		StringBuffer sf = new StringBuffer();
		sf.append("from ChannelVO");
		String condition = bulidCondition(model,server);
		sf.append(condition);
		sf.append(" order by id desc");
		String hql = sf.toString();
		String countHql = "select count(*) " + hql;
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO(ChannelVO.class);
		Query query = dao.getSession().createQuery(hql);
		if(iDisplayLength != 0)
		{
			query.setFirstResult(iDisplayStart);
			query.setMaxResults(iDisplayLength);
		}
		addTimeParm(model,query);
		List<ChannelVO> list = query.list();
		List<TableVO> returnList = new ArrayList<TableVO>();
		TableVO itemVo;
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
		Map<Long ,TableVO> map = new HashMap<Long ,TableVO>();
		for(ChannelVO channel :list)
		{
			itemVo = new TableVO();
			
			itemVo.setCount(channel.getCount());	
			if (null != channel.getDate()) {
				String date = dateformat.format(channel.getDate());
				itemVo.setDate(date);
			}
			itemVo.setChannel(channel.getName());
			itemVo.setId(channel.getLongId());
			itemVo.setParentid(channel.getParentid());
			itemVo.setStatus(channel.getStatus());
			itemVo.setShowwz(channel.getShowwz());
			returnList.add(itemVo);
		}
		
		Query countQuery = dao.getSession().createQuery(countHql);
		addTimeParm(model,countQuery);
		Object obj = countQuery.uniqueResult();
		if(obj instanceof Long)
		{
			int count = ((Long)obj).intValue();
			
			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		}
		return returnList;
	}
	

	private void addTimeParm(getChannelModel model, Query query) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		String startTimeStr = model.getStartTime();
		String endTimeStr = model.getEndTime();
		try {
			if (!StringUtils.isEmpty(startTimeStr)) {
				query.setTimestamp("startime", format.parse(startTimeStr));
			}
			if (!StringUtils.isEmpty(endTimeStr)) {
				query.setTimestamp("endtime", format.parse(endTimeStr));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	private String bulidCondition(getChannelModel model, CommonService server) {
		StringBuffer condition = new StringBuffer();
		
		String channel = model.getChannel();
		if (!StringUtils.isEmpty(channel)) {
			condition.append(" where name like '%" + channel + "%'");
		}else {
			condition.append(" where 1=1");
		}
		
		Integer status = model.getStatus();
		if (null != status) {
			condition.append(" and status = " + status + "");
		}
		
		String getAllList = model.getGetAllList();
		if(null != getAllList){
			if(Integer.valueOf(getAllList) == 0){
				condition.append(" and parentid = 0");
			}else{
				condition.append(" and parentid > 0");
			}
		}else{
			Long parentid = model.getParentid();
			if (null != parentid) {
				condition.append(" and parentid = " + parentid + "");
			}else{
				condition.append(" and parentid > 0");
			}
		}
		
		String startTimeStr = model.getStartTime();
		if(!StringUtils.isEmpty(startTimeStr)) {
			condition.append(" and date > :startime");
		}
		
		String endTimeStr = model.getEndTime();
		if(!StringUtils.isEmpty(endTimeStr)) {
			condition.append(" and date <= :endtime");
		}
		return condition.toString();
	}
}
